﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PrintDemo.Entity;
using MySql.Data.MySqlClient;
using PrintDemo.DBUtil;

namespace PrintDemo.DAL
{
    public class OrderDAL
    {
        public List<OrderEntity> GetList()
        {
            MySqlConnection conn = null;
            MySqlDataReader reader = null;
            List<OrderEntity> orders = new List<OrderEntity>();
            try
            {
                string select = "select Id,OrderId,ProcessFlowId,LineType,FinishedProductName,SemiFinishedProductName,Batch,Customer,PlanNums,DomesticAndExportSales,RequestedDate,Operator,CreateTime,UpdateTime,JuanNum,DanZhong from Orders";
                reader = SQLFactory.GetMySqlHelper().ExecuteSqlForReader(select, out conn);
                while (reader.Read())
                {
                    OrderEntity order = new OrderEntity();
                    if (!reader.IsDBNull(0))
                    {
                        order.Id = reader.GetString("Id");
                    }
                    if (!reader.IsDBNull(1))
                    {
                        order.OrderId = reader.GetString("OrderId");
                    }
                    if (!reader.IsDBNull(2))
                    {
                        order.ProcessFlowId = reader.GetString("ProcessFlowId");
                    }
                    if (!reader.IsDBNull(3))
                    {
                        order.LineType = reader.GetString("LineType");
                    }
                    if (!reader.IsDBNull(4))
                    {
                        order.FinishedProductName = reader.GetString("FinishedProductName");
                    }
                    if (!reader.IsDBNull(5))
                    {
                        order.SemiFinishedProductName = reader.GetString("SemiFinishedProductName");
                    }
                    if (!reader.IsDBNull(6))
                    {
                        order.Batch = reader.GetString("Batch");
                    }
                    if (!reader.IsDBNull(7))
                    {
                        order.Customer = reader.GetString("Customer");
                    }

                    if (!reader.IsDBNull(8))
                    {
                        order.PlanNums = reader.GetInt32("PlanNums");
                    }
                    if (!reader.IsDBNull(9))
                    {
                        order.DomesticAndExportSales = reader.GetString("DomesticAndExportSales");
                    }
                    if (!reader.IsDBNull(10))
                    {
                        order.RequestedDate = reader.GetDateTime("RequestedDate");
                    }
                    if (!reader.IsDBNull(11))
                    {
                        order.Operator = reader.GetString("Operator");
                    }
                    if (!reader.IsDBNull(12))
                    {
                        order.CreateTime = reader.GetDateTime("CreateTime");
                    }
                    if (!reader.IsDBNull(13))
                    {
                        order.UpdateTime = reader.GetDateTime("UpdateTime");
                    }
                    if (!reader.IsDBNull(14))
                    {
                        order.JuanNum = reader.GetString("JuanNum");
                    }
                    orders.Add(order);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                if (reader != null)
                    reader.Close();
                if (conn != null)
                    conn.Close();
            }
            return orders;
        }
        public List<OrderEntity> GetListByWhere(string strWhere)
        {
            MySqlConnection conn = null;
            MySqlDataReader reader = null;
            List<OrderEntity> orders = new List<OrderEntity>();
            try
            {
                string select = "select Id,OrderId,ProcessFlowId,LineType,FinishedProductName,SemiFinishedProductName,Batch,Customer,PlanNums,DomesticAndExportSales,RequestedDate,Operator,CreateTime,UpdateTime,JuanNum,DanZhong from Orders";
                if (!string.IsNullOrEmpty(strWhere))
                {
                    select += " where " + strWhere;
                }
                reader = SQLFactory.GetMySqlHelper().ExecuteSqlForReader(select, out conn);
                while (reader.Read())
                {
                    OrderEntity order = new OrderEntity();
                    if (!reader.IsDBNull(0))
                    {
                        order.Id = reader.GetString("Id");
                    }
                    if (!reader.IsDBNull(1))
                    {
                        order.OrderId = reader.GetString("OrderId");
                    }
                    if (!reader.IsDBNull(2))
                    {
                        order.ProcessFlowId = reader.GetString("ProcessFlowId");
                    }
                    if (!reader.IsDBNull(3))
                    {
                        order.LineType = reader.GetString("LineType");
                    }
                    if (!reader.IsDBNull(4))
                    {
                        order.FinishedProductName = reader.GetString("FinishedProductName");
                    }
                    if (!reader.IsDBNull(5))
                    {
                        order.SemiFinishedProductName = reader.GetString("SemiFinishedProductName");
                    }
                    if (!reader.IsDBNull(6))
                    {
                        order.Batch = reader.GetString("Batch");
                    }
                    if (!reader.IsDBNull(7))
                    {
                        order.Customer = reader.GetString("Customer");
                    }

                    if (!reader.IsDBNull(8))
                    {
                        order.PlanNums = reader.GetInt32("PlanNums");
                    }
                    if (!reader.IsDBNull(9))
                    {
                        order.DomesticAndExportSales = reader.GetString("DomesticAndExportSales");
                    }
                    if (!reader.IsDBNull(10))
                    {
                        order.RequestedDate = reader.GetDateTime("RequestedDate");
                    }
                    if (!reader.IsDBNull(11))
                    {
                        order.Operator = reader.GetString("Operator");
                    }
                    if (!reader.IsDBNull(12))
                    {
                        order.CreateTime = reader.GetDateTime("CreateTime");
                    }
                    if (!reader.IsDBNull(13))
                    {
                        order.UpdateTime = reader.GetDateTime("UpdateTime");
                    }
                    if (!reader.IsDBNull(14))
                    {
                        order.JuanNum = reader.GetString("JuanNum");
                    }
                    orders.Add(order);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                if (reader != null)
                    reader.Close();
                if (conn != null)
                    conn.Close();
            }
            return orders;
        }
    }
}
